#include "EventLoopThreadPool.hpp"

EventLoopThreadPool::EventLoopThreadPool(int numThreads) : numThreads_(numThreads), index_(0) {
    threads_.reserve(numThreads);
    for (int i = 0; i < numThreads_; ++i) {
        std::shared_ptr<EventLoopThread> t(new EventLoopThread());
        threads_.emplace_back(t);
    }
}

void EventLoopThreadPool::start() {
    for (auto& thread : threads_) {
        thread->startLoop();
    }
}


sp_EventLoop EventLoopThreadPool::getNextLoop() {
    // 类似于与环形数组的遍历
    index_ = (index_ + 1) % numThreads_;
    return threads_[index_]->getLoop();
}

void EventLoopThreadPool::stop() {
    for (auto& thread : threads_) {
        thread->stop();
    }
    for (auto& thread : threads_) {
        thread->join();
    }
    std::cout << "EventLoopThreadPool stopped" << std::endl;
}